package com.runvp.rest.app;

import com.alibaba.fastjson.JSONObject;
import com.runvp.common.annotation.Log;
import com.runvp.common.config.NettyConfig;
import com.runvp.common.constant.ConstantRedisKey;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.redis.RedisCache;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.enums.NettyCmd;
import com.runvp.common.hex.WeightCalcUtil;
import com.runvp.common.util.BigDecimalUtil;
import com.runvp.common.util.OrderNoUtils;
import com.runvp.common.utils.uuid.IdUtils;
import com.runvp.common.utils.validtion.CheckException;
import com.runvp.handler.SendCmdHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@ResponseBody
@RequestMapping("/app/client/deb")
public class ClientControllerTest {


    @Autowired
    private RedisCache redisUtil;

    /**
     *  配置刷新
     */
    @GetMapping("/getCode")
    public AjaxResult getCode(String sn) {
        String outCode = IdUtils.fastSimpleUUID();
        redisUtil.setCacheObject(ConstantRedisKey.AUTH_OUT+sn,outCode,3600);
        return AjaxResult.success("获取成功",outCode);
    }
    /**
     * 发送指令
     */
    @Log(title = "发送指令-调试", businessType = BusinessType.OTHER)
    @GetMapping("/{cmd}")
    public AjaxResult executeCmd(@PathVariable("cmd") String cmd, String baseSn, String param) throws InterruptedException {
        String orderNo = OrderNoUtils.getOrderNo();
//        orderNo = "86572978102991";
        log.info("client:::cmd:{},baseSn:{},param:{},orderNo:{}", cmd, baseSn, param, orderNo);
        CheckException.strEmptyThrowException(baseSn, "设备SN号为空");
        String body = param + orderNo;
        boolean result = false;
        String msg;
        if (NettyCmd.OUT.getCode().equals(cmd)) {
            result = new SendCmdHandler(redisUtil).sendOutCmd(orderNo, orderNo, baseSn, body);
        }
        if (result) {
            msg = "订单【" + orderNo + "】" + NettyCmd.getDesc(cmd) + "指令发成功";
            return AjaxResult.success(msg, orderNo);
        }
        String errorMsg = "订单【" + orderNo + "】" + NettyCmd.getDesc(cmd) + "指令发失败或超时";
        log.error(errorMsg);
        return AjaxResult.error(errorMsg);
    }

    /**
     * 查询订单
     */
    @Log(title = "订单查询-调试", businessType = BusinessType.OTHER)
    @GetMapping("/queryOrder/{orderNo}")
    public AjaxResult queryOrder(@PathVariable("orderNo") String orderNo) {
        log.info("client:::orderNo: {}", orderNo);
        String res = redisUtil.getCacheObject(ConstantRedisKey.ORDER + orderNo);
        if (StringUtils.isBlank(res)) {
            return AjaxResult.error("未查到相关信息");
        }
        return AjaxResult.success(JSONObject.parseObject(res));
    }

    /**
     * todo 10进制数字换算16进制脉冲字符串
     */
    @Log(title = "10进制数字换算16进制脉冲字符串", businessType = BusinessType.OTHER)
    @GetMapping("/calc/{weight}")
    public AjaxResult clac(@PathVariable("weight") BigDecimal weight, Integer type) {
        log.info("client:::weight: {}", weight);
        Map<String, Object> map = new HashMap<>();
        long pulseNum;
        //1代表只计算全部
        if (type == null || type == 1) {
            pulseNum = BigDecimalUtil.mul(weight, BigDecimalUtil.vf(NettyConfig.getPulseConversion())).longValue();
        } else {
            pulseNum = weight.longValue();
        }
        map.put("pulseNum", pulseNum);
        map.put("pulseNumHex", WeightCalcUtil.decimal2Hex(pulseNum));
        return AjaxResult.success("换算成功", map);
    }
}
